package com.imc.platform.base.teacher.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.imc.core.utils.MenuUtils;
import com.imc.core.utils.MysqlUtil;
import com.imc.platform.base.teacher.service.ITeacherService;
import com.imc.platform.base.user.pojo.User;
import com.imc.platform.base.user.pojo.UserDesc;
import com.twp.common.tuple.ResultDTO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.List;
import java.util.Map;

/**
 * 类描述：校长管理
 * 开发者：QY
 * 创建时间：2018/9/3 8:00
 **/
@Slf4j
@Controller
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    ITeacherService iTeacherService;

    @Autowired
    MenuUtils menuUtils;

    @Autowired
    MysqlUtil mysqlUtil;

    /**
     * QY 2018/9/3 8:01
     * 方法描述：传参对象
     **/
    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    public static class ReqTuple {
        Map <String, Object> parmsMap;
        List <Map <String, Object>> parmsList;
        String userId;
        UserDesc userDesc;
    }

    /**
     * QY 2018/9/3 8:32
     * 方法描述：跳转用户页面
     **/
    @RequestMapping(value = "/showTeacherPage", method = RequestMethod.GET)
    public String showTeacherPage(String menuId, Model model, HttpServletRequest request) {
        model.addAttribute("menu", menuUtils.getMenuByRoleId(request, menuId));
        model.addAttribute("menuId", menuId);
        model.addAttribute("type", "2");//老师管理
        model.addAttribute("stype", "2");//校长管理
        return "base/user/user";
    }

    /**
     * QY 2018/9/3 10:16
     * 方法描述：查询所有用户
     **/
    @RequestMapping(value = "/queryTeacherUser", method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO queryTeacherUser(Principal principal, Pageable pageable) {
        String uname = principal.getName();
        User user = mysqlUtil.getUserByLoginNameCommon(uname);
        String duty = user.getDuty();
        String duty2 = "";
        String orgId = user.getOrgId();

        PageInfo <User> queryUserList = iTeacherService.queryTeacherUser(duty,orgId,pageable.getPageNumber(), pageable.getPageSize());

        JSONObject map = new JSONObject();
        map.put("total", queryUserList.getTotal());
        map.put("rows", queryUserList.getList());
        map.put("pageNumber", queryUserList.getPageNum());
        map.put("pageSize", queryUserList.getPageSize());
        ResultDTO result = ResultDTO.builder().data(map).build();
        return result;
    }

}
